package com.chenglu.android.mine.member;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;

import com.chenglu.android.R;
import com.chenglu.android.common.base.BaseFullScreenActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.GlideUtils;
import com.chenglu.android.common.utils.StatusBarUtil;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.common.widget.RadiusImageView;
import com.chenglu.android.mine.bean.FansListBean;
import com.chenglu.android.mine.bean.MemberCenterBean;
import com.chenglu.android.mine.bean.PosterBean;
import com.chenglu.android.mine.bean.ReturnStringBean;
import com.chenglu.android.mine.member.mvp.contract.MemberContract;
import com.chenglu.android.mine.member.mvp.presenter.MemberPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: lyj
 * @Created: by Copyright 2023 6 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：会员中心
 */
public class MemberCenterActivity extends BaseFullScreenActivity<MemberPresenter> implements MemberContract.View {

    public static final String HEADPHOTO = "HEADPHOTO";
    public static final String NICKNAME = "NICKNAME";

    @BindView(R.id.iv_head_pic)
    RadiusImageView ivHeadPic;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_level_member_title)
    TextView tvLevelMemberTitle;
    @BindView(R.id.tv_v_level)
    TextView tvVLevel;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R.id.ll_loading_data)
    LinearLayout llLoadingData;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_loading_no_data)
    LinearLayout llLoadingNoData;
    @BindView(R.id.tv_level_list)
    TextView tvLevelList;
    @BindView(R.id.tv_empiric_value)
    TextView tvEmpiricValue;
    @BindView(R.id.tv_fans_list)
    TextView tvFansList;
    @BindView(R.id.ll_fans_pic)
    LinearLayout llFansPic;
    @BindView(R.id.tv_consume_list)
    TextView tvConsumeList;
    @BindView(R.id.tv_next_level)
    TextView tvNextLevel;
    @BindView(R.id.tv_fans_number)
    TextView tvFansNumber;
    @BindView(R.id.tv_fans_months)
    TextView tvFansMonths;
    @BindView(R.id.tv_fans_days)
    TextView tvFansDays;
    @BindView(R.id.tv_consume)
    TextView tvConsume;
    @BindView(R.id.tv_consume_months)
    TextView tvConsumeMonths;
    @BindView(R.id.tv_consume_days)
    TextView tvConsumeDays;
    @BindView(R.id.tv_consume_all)
    TextView tvConsumeAll;
    @BindView(R.id.tv_current_value)
    TextView tvCurrentValue;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.ic_back)
    ImageView icBack;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;

    private int mScrollY = 0;

    private String getHeadphoto, getNickname;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_center);
        ButterKnife.bind(this);
        StatusBarUtil.immersive(this);
        StatusBarUtil.setPaddingSmart(this, toolbar);
        initScroolview();
        mPresenter = new MemberPresenter();
        mPresenter.attachView(this);
        getHeadphoto = getIntent().getExtras().getString(HEADPHOTO);
        getNickname = getIntent().getExtras().getString(NICKNAME);
//        mPresenter.getLevelDescriptions(getToken());
        mPresenter.getMemberCenterInfo(getToken());
    }


    //初始化滑动显示和隐藏toolbar
    private void initScroolview() {


        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            int lastScrollY = 0;
            int h = Global.dp2px(60);
            int color = ContextCompat.getColor(Global.mContext, R.color.white) & 0x00ffffff;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
//                    buttonBarLayout.setAlpha(1f * mScrollY / h);
                    toolbar.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
//                    ivHeader.setTranslationY(mOffset - mScrollY);
                }
                if (scrollY == 0) {
                    tvTitle.setTextColor(getResources().getColor(R.color.transparent));


                } else {
                    tvTitle.setTextColor(getResources().getColor(R.color.all_black_text));


                }

                lastScrollY = scrollY;
            }
        });
        toolbar.setBackgroundColor(0);


    }

    @OnClick({R.id.tv_level_list, R.id.tv_current_value, R.id.tv_fans_list, R.id.ll_fans_pic, R.id.tv_consume_list, R.id.ll_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_level_list:
                startActivity(new Intent(this, MemberLevelActivity.class));
                break;
            case R.id.tv_current_value:
                Intent intent = new Intent(this, ExperienceInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(ExperienceInfoActivity.FANSDATA, memberCenterBean);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.tv_fans_list:
                startActivity(new Intent(this, FansListActivity.class));
                break;
            case R.id.ll_fans_pic:
                ToastUtils.show("近期开放");
//                startActivity(new Intent(this,InviteFansActivity.class));
                break;
            case R.id.tv_consume_list:
                startActivity(new Intent(this, ConsumeListActivity.class));
                break;
            case R.id.ll_back:
                finish();
                break;
        }
    }


    @Override
    public void getLevelDescriptions(ReturnStringBean bean) {

    }

    private MemberCenterBean memberCenterBean;

    @Override
    public void getMemberCenterInfo(MemberCenterBean bean) {
        if (bean.getCode().equals(Constant.HTTPCODE200)) {
            memberCenterBean = bean;

            llLoadingData.setVisibility(View.VISIBLE);
            llLoadingNoData.setVisibility(View.GONE);
            rlTitle.setVisibility(View.VISIBLE);

            GlideUtils.setImageView(mContext, getHeadphoto, ivHeadPic, 30); //头像
            tvNickname.setText(getNickname);

            tvLevel.setText(bean.getData().getLevelName());
            tvLevelMemberTitle.setText(bean.getData().getLevelName());//会员等级名称
            tvVLevel.setText("V" + bean.getData().getLevel());
            tvEmpiricValue.setText(bean.getData().getExperience() + "%");
            progressBar.setProgress(bean.getData().getExperience());  //经验值
            tvNextLevel.setText("升级至V" + bean.getData().getNextLevel()); //下个等级
            //粉丝数量
            tvFansNumber.setText(bean.getData().getTotalCount() + "");
            tvFansMonths.setText(bean.getData().getMonthCount() + "");//粉丝月数
            tvFansDays.setText(bean.getData().getTodayCount() + ""); //今日粉丝数

            tvConsume.setText(Utils.formatZeroNumber(bean.getData().getTotalConsumption())); //累计综合消费
            tvConsumeMonths.setText(Utils.formatZeroNumber(bean.getData().getMonthConsumption()));
            tvConsumeDays.setText(Utils.formatZeroNumber(bean.getData().getTodayConsumption()));
            tvConsumeAll.setText(Utils.formatZeroNumber(bean.getData().getMyConsumption()));

            //服务中心
            if (bean.getData().getHasMemberCard().equals("1")) {
                tvCenter.setVisibility(View.VISIBLE);
            } else {
                tvCenter.setVisibility(View.GONE);
            }


        } else {
            llLoadingData.setVisibility(View.GONE);
            llLoadingNoData.setVisibility(View.VISIBLE);
            rlTitle.setVisibility(View.GONE);
        }

    }

    @Override
    public void getFansPage(FansListBean bean) {

    }

    @Override
    public void getConsumerPage(FansListBean bean) {

    }

    @Override
    public void getPromotionPoster(PosterBean bean) {

    }
}
